package com.hins.demo.gatewayimpl;

import com.hins.demo.convertor.AccountConvertor;
import com.hins.demo.dao.AccountMapper;
import com.hins.demo.dao.dataobject.AccountDO;
import com.hins.demo.gateway.TransferGateway;
import com.hins.demo.model.transfer.entity.Account;
import com.hins.demo.model.transfer.valueobject.AccountId;
import com.hins.demo.model.transfer.valueobject.AccountNumber;
import com.hins.demo.model.transfer.valueobject.UserId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description: 666
 *
 * @author wenjiaqi
 * @date 2021/5/14 19:15
 */
@Component
public class TransferGatewayImpl implements TransferGateway {

    @Autowired
    private AccountMapper accountDAO;

    @Autowired
    private AccountConvertor accountConvertor;

    @Override
    public Account find(AccountId accountId) {
        AccountDO accountDO = accountDAO.selectById(accountId.getValue());
        return accountConvertor.toAccount(accountDO);
    }

    @Override
    public Account find(AccountNumber accountNumber) {
        AccountDO accountDO = accountDAO.selectByAccountNumber(accountNumber.getValue());
        return accountConvertor.toAccount(accountDO);
    }

    @Override
    public Account find(UserId userId) {
        AccountDO accountDO = accountDAO.selectByUserId(userId.getValue());
        return accountConvertor.toAccount(accountDO);
    }

    @Override
    public Account save(Account account) {
        AccountDO accountDO = accountConvertor.toAccountDO(account);
        if (accountDO.getAccountId() == null) {
            accountDAO.insert(accountDO);
        } else {
            accountDAO.update(accountDO);
        }
        return accountConvertor.toAccount(accountDO);
    }
}
